﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmDiemSV : Form
    {
        private static frmDiemSV frm = null;

        private Diem DiemSV;

        private frmDiemSV()
        {
            InitializeComponent();
        }

        public static frmDiemSV Create()
        {
            return Create(false);
        }

        public static frmDiemSV Create(bool child)
        {
            if (frm == null)
            {
                frm = new frmDiemSV();
            }
            if (child)
            {
                frm.ControlBox = false;
                frm.MinimizeBox = false;
                frm.MaximizeBox = false;
                frm.WindowState = FormWindowState.Maximized;
            }
            return frm;
        }

        private void frmDiem_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void frmDiem_Load(object sender, EventArgs e)
        {
            
        }

        private void txtMaSV_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnTim.PerformClick();
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            lblMaSV.Text = txtMaSV.Text;
            DiemSV = new Diem(lblMaSV.Text);
            dgvDiem.DataSource = DiemSV.ListAll(TypeDiem.DiemSV);
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            SinhVien sv = new SinhVien();
            sv.Info(lblMaSV.Text);
            //frmReportDíemSinhVien frm = new frmReportDíemSinhVien((new DiemSinhVien(txtMaSV.Text)).ListAll());
            frmReportDíemSinhVien frm = new frmReportDíemSinhVien((DataTable)dgvDiem.DataSource, sv);
            frm.ShowDialog();
        }

        private void dgvDiem_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

        }
        
        private void dgvDiem_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            Diem diemsv = new Diem();
            int check = diemsv.UpdateDiem(int.Parse(dgvDiem.Rows[index].Cells["cMaDK"].Value.ToString()),
                dgvDiem.Rows[index].Cells["cDiemQT"].Value.ToString().Replace(',', '.'),
                dgvDiem.Rows[index].Cells["cDiemT"].Value.ToString().Replace(',', '.'));
            DataTable temp = diemsv.Detail(int.Parse(dgvDiem.Rows[index].Cells["cMaDK"].Value.ToString()));
            if (check > 0)
            {
                DataTable diem = (DataTable)dgvDiem.DataSource;
                DataRow dr = diem.NewRow();
                dr[0] = temp.Rows[0]["MaDK"];
                dr[1] = temp.Rows[0]["MaMH"];
                dr[2] = temp.Rows[0]["TenMH"];
                dr[3] = temp.Rows[0]["MaLopDK"];
                dr[4] = temp.Rows[0]["TenLopDK"];
                dr[5] = temp.Rows[0]["DiemQT"];
                dr[6] = temp.Rows[0]["DiemT"];
                dr[7] = temp.Rows[0]["DiemTB"];
                dr[8] = temp.Rows[0]["Loai"];
                dr[9] = temp.Rows[0]["NamHoc"];
                dr[10] = temp.Rows[0]["HocKy"];
                diem.Rows.InsertAt(dr, index + 1);
                diem.Rows.RemoveAt(index);
            }
            else
            {
                dgvDiem.Rows[index].Cells["cDiemQT"].Value = temp.Rows[0]["DiemQT"];
                dgvDiem.Rows[index].Cells["cDiemT"].Value = temp.Rows[0]["DiemT"];
                MessageBox.Show("Bạn phải nhập đúng điểm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
